package com.quectel.taskserver.job.partybuilding;

import cn.hutool.core.lang.func.LambdaUtil;
import com.quectel.business.common.partybuilding.PartyPersonMsgBusiness;
import com.quectel.constant.PushConstants;
import com.quectel.constant.core.mobileuser.MobileUserMsgConstants;
import com.quectel.constant.core.partybuilding.PartyBuildingConstants;
import com.quectel.constant.core.partybuilding.PartyBuildingPlanConstants;
import com.quectel.constant.global.SystemConstants;
import com.quectel.core.module.mobileuser.dto.MobileUserDto;
import com.quectel.core.module.mobileuser.service.MobileUserService;
import com.quectel.core.module.partybuilding.dto.PartyPersonDto;
import com.quectel.core.module.partybuilding.service.PartyPersonService;
import com.quectel.core.module.partybuildingtask.dto.PartyBuildingPlanTaskDto;
import com.quectel.core.module.partybuildingtask.service.PartyBuildingPlanService;
import com.quectel.core.module.partybuildingtask.service.PartyBuildingPlanTaskService;
import com.quectel.util.common.DateUtils;
import com.quectel.util.kit.PageFetchKit;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.shardingsphere.elasticjob.api.ShardingContext;
import org.apache.shardingsphere.elasticjob.simple.job.SimpleJob;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.*;


/**
 * 党建任务生成
 *
 * @author: bob
 * @email: bob.yu@quectel.com
 * @date: 2023-06-12 8:59:00
 */
@Component
public class PartyBuildingPlanTaskRemindTask implements SimpleJob {

    private static final Logger LOGGER = LoggerFactory.getLogger(PartyBuildingPlanTaskRemindTask.class);

    public static final String GEN_TASK_TIME = "genTaskTime";

    @DubboReference
    private PartyBuildingPlanService partyBuildingPlanService;
    @DubboReference
    private PartyBuildingPlanTaskService partyBuildingPlanTaskService;
    @DubboReference
    private PartyPersonService partyPersonService;
    @DubboReference
    private MobileUserService mobileUserService;
    @Autowired
    private PartyPersonMsgBusiness partyPersonMsgBusiness;

    @Override
    public void execute(ShardingContext shardingContext) {
        LOGGER.info("党建任务提醒，开始执行... 时间 {}", DateUtils.getCurrentDateStr(DateUtils.FormatType.COMMON));

        //查询当前时间大于提醒时间并且未提醒的
        Map<String,Object> params = new HashMap<>(4);
        params.put(LambdaUtil.getFieldName(PartyBuildingPlanTaskDto::getIsPush),SystemConstants.NO);
        params.put(LambdaUtil.getFieldName(PartyBuildingPlanTaskDto::getRemindTime),DateUtils.format(new Date(), DateUtils.FormatType.COMMON));
        params.put(LambdaUtil.getFieldName(PartyBuildingPlanTaskDto::getExecStatus), PartyBuildingPlanConstants.TaskStatus.UNCOMPLETED.getStatus());

        List<PartyBuildingPlanTaskDto> partyBuildingPlanDtos = PageFetchKit.fetchAll(r -> partyBuildingPlanTaskService.queryList(r), params);
        if(CollectionUtils.isEmpty(partyBuildingPlanDtos)){
            LOGGER.info("党建任务提醒，结束执行... 时间 {}", DateUtils.getCurrentDateStr(DateUtils.FormatType.COMMON));
            return;
        }

        for (PartyBuildingPlanTaskDto partyBuildingPlanTaskDto : partyBuildingPlanDtos) {
            PartyPersonDto partyPersonDto = partyPersonService.selectCacheById(partyBuildingPlanTaskDto.getExecUserId());
            if(partyPersonDto==null){
                continue;
            }

            MobileUserDto mobileUserDto = mobileUserService.selectByMobile(partyPersonDto.getMobile());
            if(mobileUserDto==null){
                continue;
            }

            partyPersonMsgBusiness.pushMsg(
                    partyBuildingPlanTaskDto.getTenantId(),
                    Collections.singletonList(mobileUserDto),
                    MobileUserMsgConstants.SourceType.PARTY_BUILDING_TASK_MSG,
                    partyBuildingPlanTaskDto.getId(),
                    PartyBuildingConstants.PUSH_MSG_PARTY_BUILDING_TASK_TITLE,
                    partyBuildingPlanTaskDto.getName(),
                    true,
                    new PushConstants.PushType[]{PushConstants.PushType.WECHAT_OFFICIAL}
            );
            partyBuildingPlanTaskDto.setIsPush(SystemConstants.YES);
            partyBuildingPlanTaskService.updateById(partyBuildingPlanTaskDto);
        }
        LOGGER.info("党建任务提醒，结束执行... 时间 {}", DateUtils.getCurrentDateStr(DateUtils.FormatType.COMMON));
    }


}
